namespace Leetcode.N0427;
[System.Diagnostics.DebuggerDisplay("isLeaf:{isLeaf} val:{val}")]
public class Node
{
    public bool val;
    public bool isLeaf;
    public Node topLeft;
    public Node topRight;
    public Node bottomLeft;
    public Node bottomRight;

    public Node()
    {
        val = false;
        isLeaf = false;
        topLeft = null;
        topRight = null;
        bottomLeft = null;
        bottomRight = null;
    }

    public Node(bool _val, bool _isLeaf)
    {
        val = _val;
        isLeaf = _isLeaf;
        topLeft = null;
        topRight = null;
        bottomLeft = null;
        bottomRight = null;
    }

    public Node(bool _val, bool _isLeaf, Node _topLeft, Node _topRight, Node _bottomLeft, Node _bottomRight)
    {
        val = _val;
        isLeaf = _isLeaf;
        topLeft = _topLeft;
        topRight = _topRight;
        bottomLeft = _bottomLeft;
        bottomRight = _bottomRight;
    }

    public override string ToString()
    {
        var builder = new System.Text.StringBuilder();
        builder.Append('[');

        var queue = new Queue<Node>();
        queue.Enqueue(this);

        while (queue.Count > 0)
        {
            var list = new List<Node>();
            while (queue.Count > 0)
            {
                list.Add(queue.Dequeue());
            }

            foreach (var item in list)
            {
                if (item == null)
                {
                    builder.Append("null,");
                }
                else
                {

                    builder.Append($"[{(item.isLeaf ? 1 : 0)},{(item.val ? 1 : 0)}],");
                    queue.Enqueue(item.topLeft);
                    queue.Enqueue(item.topRight);
                    queue.Enqueue(item.bottomLeft);
                    queue.Enqueue(item.bottomRight);
                }
            }
        }

        return builder.ToString().TrimEnd(',', 'n', 'u', 'l') + ']';
    }
}